package com.byq.mm.smartlib.cluster.activity.client;

import java.util.LinkedList;
import java.util.List;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;

import com.byq.mm.smartlib.R;
import com.byq.mm.abstractx.cluster.client.event.ClusterChangeEvent;
import com.byq.mm.application.cluster.client.PictureClient;
import com.byq.mm.smartlib.cluster.AnimationController;

public class ClientPictureActivity extends ClientClusterActivity implements View.OnClickListener{

	List<Bitmap> images;
	Gallery gallery;
	private ViewGroup mContainer;
	private ImageView mImageView;

	@Override
	public Object onRetainNonConfigurationInstance() {
	   
	    return images;
	}
	private void loadPhotos() {
		images = (List<Bitmap>)getLastNonConfigurationInstance();
	   
	    if (images == null) {
	    	images = new LinkedList<Bitmap>();
	    } 
	}

	 @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        
	        setContentView(R.layout.cluster_picture);
	  

	        gallery = (Gallery)findViewById(R.id.mypictures);
	    	mImageView = (ImageView) findViewById(R.id.picture);
			mContainer = (ViewGroup) findViewById(R.id.container);
			
			gallery.setOnItemClickListener(new OnItemClickListener() {
				public void onItemClick(AdapterView parent, View v, int position,
						long id) {
					ImageView i = (ImageView) v;
					mImageView.setImageDrawable(i.getDrawable());
					new AnimationController(mContainer, gallery, mImageView, true).flap();
				}
			});
			// Prepare the ImageView
			mImageView.setClickable(true);
			mImageView.setFocusable(true);
			mImageView.setOnClickListener(this);
			
	
	        Button btn = (Button)findViewById(R.id.forcepicture); 
	        btn.setOnClickListener(new View.OnClickListener() 
	        { 
	                public void onClick(View v) 
	                { 
	                	PictureClient pc = (PictureClient)client;
	                	pc.force();
	                } 
	        }); 
	        btn=(Button)findViewById(R.id.requestpicture); 
	        btn.setOnClickListener(new View.OnClickListener() 
	        { 
	                public void onClick(View v) 
	                { 
	                	PictureClient pc = (PictureClient)client;
	                	pc.request();
	                } 
	        }); 

	      
			this.loadPhotos();
			gallery.setAdapter(new ImageAdapter(this, images.toArray(new Bitmap[0])));

			
	    }
	
	


		private MenuItem pictures;
	

		public void onClick(View v) {
			new AnimationController(mContainer, gallery, mImageView, false).flap();
		}
		@Override
		public boolean onCreateOptionsMenu(Menu menu) {

				boolean result = super.onCreateOptionsMenu(menu);

			
				pictures = menu.add("ͼƬ����");
			
			return result;
		}

		@Override
		public boolean onOptionsItemSelected(MenuItem item) {
			int i = item.getItemId();
			if(pictures == item){
//				Intent intent = new Intent();
//		        intent.putExtra("entity", account);
//		        intent.setClass(this, PictureGallery.class);
//		       // intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//				
//				startActivity(intent);
			
			}
		
			return true;
			
		}
//		BroadcastReceiver receiver = new BroadcastReceiver(){
//
//			@Override
//			public void onReceive(Context context, Intent intent) {
//				Report r =  (Report)intent.getExtras().get("entity");
//				if(!r.from.equals(account.getToId()))
//					return;
//				if(r.action.equals(ProtocolMessage.COMMAND_PIC)){
//				 String url = (String)intent.getExtras().get("picture");
//				 Bitmap bm = null;
//					try {
//						bm = BitmapFactory.decodeStream(new URL(url).openStream());
//						images.add(0,bm);
//						gallery.setAdapter(new ImageAdapter(PhonePictureActivity.this, images.toArray(new Bitmap[0])));
//						
//					} catch (Exception e) {
//						// TODO Auto-generated catch block
//						e.printStackTrace();
//					} 
//				}else{
//					
//				}
//			}
//			
//		};


class ImageAdapter extends BaseAdapter {
	int mGalleryItemBackground;
	Bitmap[] images;

	public ImageAdapter(Context c, Bitmap[] ims) {
		images = ims;
		mContext = c;
		// See res/values/attrs.xml for the <declare-styleable> that defines
		// Gallery1.
		TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
		mGalleryItemBackground = a.getResourceId(
				R.styleable.Gallery1_android_galleryItemBackground, 0);
		a.recycle();
	}

	public int getCount() {
		return images.length;
	}

	public Object getItem(int position) {
		return position;
	}

	public long getItemId(int position) {
		return position;
	}


	public View getView(int position, View convertView, ViewGroup parent) {
		ImageView i = new ImageView(mContext);
		
		i.setImageBitmap(images[position]);
		i.setScaleType(ImageView.ScaleType.FIT_XY);
		i.setLayoutParams(new Gallery.LayoutParams(150, 100));
		i.setBackgroundResource(mGalleryItemBackground);

		return i;
	}

	private Context mContext;

}
@Override
protected void update(ClusterChangeEvent e) {
	// TODO Auto-generated method stub
	
}


}
